//
//  MainViewController.m
//  Remind
//
//  Created by mac on 17/9/27.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "MainViewController.h"
#import "EventTableView.h"
#import "MainScrollView.h"
#import "SortNavigation.h"
#import "EeventDetailsViewController.h"
#import "LGSideMenuController.h"
#import "UIViewController+LGSideMenuController.h"
#import "DataCach.h"
#import "AddEventViewController.h"

#define TABEL_TAG 1998
@interface MainViewController () <UIScrollViewDelegate>
{
    NSArray *classNameArr;
    UIColor *mainColor;
}
@property (strong, nonatomic) MainScrollView *mainScroll;
@property (strong, nonatomic) UIScrollView *navScroll;
@property (strong, nonatomic) SortNavigation *sortNav;

@property (strong, nonatomic) NSMutableArray *tableViewArr;
@property (strong, nonatomic) NSMutableArray *btnArr;

@property (strong, nonatomic) NSNotificationCenter *notificationCenter;

@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated{
    //移除消息
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"reFreshTableView" object:nil];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFreshTableView:) name:@"reFreshTableView" object:nil];
}

- (void)loadData{
    _tableViewArr = [NSMutableArray new];
//    _notificationCenter = [NSNotificationCenter defaultCenter];
    
    mainColor = [UIColor colorWithRed:20/255.0 green:150/255.0 blue:200/255.0 alpha:1.0];
    
    classNameArr = [DataCach GetTheSortedArray];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // ----
    self.title = [[DataCach getUserInformation] objectForKey:@"name"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"菜单"] style:UIBarButtonItemStylePlain target:self action:@selector(showLeftMenu)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent)];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    // ----
    [self loadData];
    
    self.view.backgroundColor = [UIColor blackColor];
    CGFloat width = self.view.frame.size.width;
    _mainScroll = [[MainScrollView alloc] initWithFrame:CGRectMake(0, 112, width, self.view.frame.size.height - 48 - 64)];
    _mainScroll.contentSize = CGSizeMake(width * classNameArr.count, 0);
    _mainScroll.showsVerticalScrollIndicator = NO;
    _mainScroll.showsHorizontalScrollIndicator = NO;
    _mainScroll.pagingEnabled = YES;
    _mainScroll.userInteractionEnabled = YES;
    _mainScroll.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    //_mainScroll.backgroundColor = [UIColor whiteColor];
    _mainScroll.delegate = self;
    [self.view addSubview:_mainScroll];
    
    _sortNav = [[SortNavigation alloc] initWithFrame:CGRectMake(0, 64, width, 48) Titles:classNameArr DefaultIndex:0];
   
    MainViewController __weak  *me = self;
    _sortNav.clickBlock = ^(NSInteger idx) {
         [me.mainScroll setContentOffset:CGPointMake(width * idx, 0) animated:YES];
    };
    [self.view addSubview:_sortNav];
    [self addTableViewInTheMainScrollView];
}

//左边侧滑栏
- (void)showLeftMenu{
    LGSideMenuController *sideMenu = [self sideMenuController];
    [sideMenu showLeftViewAnimated:YES completionHandler:nil];
}

#pragma mark - 跳转添加页面
- (void)addEvent{
    AddEventViewController *addEventViewCon = [[AddEventViewController alloc] init];
    [self.navigationController pushViewController:addEventViewCon animated:YES];
    NSLog(@"_____---->注册消息");
}

- (void)reFreshTableView:(NSNotification *)notification{
    if ([[NSThread currentThread] isMainThread]) {
        NSLog(@"______---->main");
    } else {
        NSLog(@"______---->not main");
    }
    int index = (int)[classNameArr indexOfObject:notification.object];
    NSLog(@"++++++收到消息刷新%d",index);
    //移除消息
    [_notificationCenter removeObserver:self name:@"reFreshTableView" object:nil];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        for(EventTableView *tab in _tableViewArr){
            if(tab.tag - TABEL_TAG == index){
                [tab reloadDatas];
            }
        }
        
    });
}

- (void)addTableViewInTheMainScrollView{
    CGFloat width = self.view.frame.size.width;
    NSInteger idx = _mainScroll.contentOffset.x / width;
    
    for (EventTableView *table in _tableViewArr) {
        NSInteger tag =  table.tag - TABEL_TAG;
        if(tag == idx) return ;
    }

    EventTableView *tableView = [[EventTableView alloc] initWithFrame:CGRectMake(idx * width, 8, width, _mainScroll.frame.size.height) ClassName:classNameArr[idx]];
    tableView.tag = TABEL_TAG + idx;
    tableView.selectedBlock = ^(NSString *key, NSString *className, NSString *massage){
#pragma mark - 跳转详情页面
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        EeventDetailsViewController *eventDetailCon = [story instantiateViewControllerWithIdentifier:@"eventDetail"];
        eventDetailCon.key = key;
        eventDetailCon.className = className;
        eventDetailCon.timeStr = massage;
        [self.navigationController pushViewController:eventDetailCon animated:YES];
        
    };
    [_tableViewArr addObject:tableView];
    [_mainScroll addSubview:tableView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger idx = scrollView.contentOffset.x / self.view.frame.size.width;
    [_sortNav setScrollViewOffsetXByIndex:idx];
    
    [self addTableViewInTheMainScrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
   
    [self addTableViewInTheMainScrollView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
